package ihistoryApp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import ihistoryApp.dal.entity.Follow;
import ihistoryApp.dal.entity.User;
import ihistoryApp.dal.mapper.FollowMapper;
import ihistoryApp.dal.mapper.UserMapper;
import ihistoryApp.request.FollowRequest;
import ihistoryApp.request.GetFollowListRequest;
import ihistoryApp.view.FollowView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class FollowService {
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UserMapper userMapper;

    public String follow(FollowRequest request) {
        if (ObjectUtil.hasNull(request, request.getFollower(), request.getFan())) {
            return "参数不正确";
        }
        LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<Follow>()
                .eq(Follow::getFan, request.getFan())
                .eq(Follow::getFollower, request.getFollower())
                ;
        Long count = followMapper.selectCount(queryWrapper);
        if (count > 0) {
            followMapper.delete(queryWrapper);
        } else {
            Follow follow = new Follow();
            BeanUtil.copyProperties(request, follow);
            followMapper.insert(follow);
        }
        return "操作完成";
    }

    public List<FollowView> getFollowList(GetFollowListRequest request) {
        LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<Follow>()
                .eq(Follow::getFan, request.getUid())
                ;
        List<Long> followList = followMapper.selectList(queryWrapper).stream().map(Follow::getFollower).toList();
        if(followList.isEmpty()) {
            return new ArrayList<>();
        }
        Map<Long, String> userMap = userMapper.selectBatchIds(followList).stream().collect(Collectors.toMap(User::getUid, User::getNickName));
        return followList.stream().map(follow -> {
            return new FollowView(follow, userMap.get(follow));
        }).toList();
    }

    public List<FollowView> getFanList(GetFollowListRequest request) {
        LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<Follow>()
                .eq(Follow::getFollower, request.getUid())
                ;
        List<Long> followList = followMapper.selectList(queryWrapper).stream().map(Follow::getFan).toList();
        if(followList.isEmpty()) {
            return new ArrayList<>();
        }
        Map<Long, String> userMap = userMapper.selectBatchIds(followList).stream().collect(Collectors.toMap(User::getUid, User::getNickName));
        return followList.stream().map(follow -> {
            return new FollowView(follow, userMap.get(follow));
        }).toList();
    }
}
